<?php
//declare (strict_types = 1);

namespace app\command;

use api\channel\meituan\distribution\Request\SeckillRequest;
use app\api\service\OrderService;
use app\command\coupon\SyncJutuike;
use app\common\facade\supplier\Supplier;
use app\common\service\finance\merchant\BalanceService;
use app\common\service\finance\merchant\WithdrawService;
use app\common\service\order\CouponSyncOrderService;
use app\common\service\order\pipeline\Request;
use app\event\OrderPayRefund;
use app\model\AgentServiceConfig;
use app\model\CouponExtension;
use app\model\CouponOrder;
use app\model\MeituanCity;
use app\model\TaototoCity;
use app\task\cron\coupon\RefundOrder;
use app\task\cron\coupon\SyncJutuikeOrder;
use Carbon\Carbon;
use Dml\EasyTBK\Factory;
use Dml\EasyTBK\TaoBao\Request\TbkItemInfoGetRequest;
use Dml\EasyTBK\TaoBao\Request\TbkOrderGetRequest;
use easyTbk\taobao\request\EleOfficialactivityGetRequest;
use easyTbk\taobao\request\EleOrderGetRequest;
use GaoweiSpace\MeituanPubUnion\Api\Common\Request\SeckillListRequest;
use GaoweiSpace\MeituanPubUnion\Api\Common\Request\SeckillShowInfoRequest;
use GaoweiSpace\MeituanPubUnion\Http\Client;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;
use think\facade\Log;
use think\Pipeline;

class Test extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('test')
            ->setDescription('the test command');
    }

    protected function execute(Input $input, Output $output)
    {

        $obj = new CouponSyncOrderService();
        $obj->products_serv = $obj->product_serv();
        $obj->syncOrder(PRODUCT_ELE_SERV);

        exit;




        $taobao = Factory::taobao([
            'app_key' => '34500897',
            'app_secret' => '81de8f2797934fb2adbb1abbbf8cb757',
            'format' => 'json'
        ]);

//        $req = new EleOfficialactivityGetRequest();//获取
        $req = new EleOrderGetRequest([
            'date_type' => 4,//，1-付款时间 2-创建时间 3-结算时间 4-更新时间
            'biz_unit' => 2,
            'page_size' => 50,
            'page_number' => 1,
            'start_date' => Carbon::now()->subDays(2)->toDateTimeString(),
            'end_date' => Carbon::now()->toDateTimeString(),
        ]);

        $data = $taobao->execute($req);

        $data = json_decode(json_encode($data), true);
        $data = $data['result'] ?? [];
        $data = $data['order_detail_report_d_t_o'] ?? [];

        var_dump($data);
        exit;

        (new CouponExtension)->getAlready();

        exit;

        $arrs = [
            'G26263330198688' => '26263330204320-50|电信SKU26263330205856-50|移动SKU26263330206880-50|联通SKU26263330207904-100|电信SKU26263330208416-100|移动SKU26263330209440-100|联通SKU26263330210464-200|电信SKU26263330211488-200|移动SKU26263330212512-200|联通',//快充
            'G25985744355488' => '25985744361632-50|电信SKU25985744363168-50|移动SKU25985744364704-50|联通SKU25985744366240-100|电信SKU25985744367776-100|移动SKU25985744369312-100|联通SKU25985744374432-200|电信SKU25985744375968-200|移动SKU25985744377504-200|联通',//慢充
        ];

        $newArray = [];
        foreach ($arrs as $productId => $item) {
            foreach (explode('SKU', $item) as $oneStrItem) {
                $oneItem = explode('-', $oneStrItem);
                $skuId = $oneItem[0];
                $name = explode('|', $oneItem[1])[1];
                $face = explode('|', $oneItem[1])[0];

                $newArray[$productId . $name . $face] = $skuId;
            }
        }

        var_dump($newArray[$productId . '联通100']);
        exit;


        $error = [];

        TaototoCity::query()
            ->where('area_id', 0)
            ->select()->each(function (TaototoCity $taototoCity) use (&$error) {
                $data = MeituanCity::query()->where('name', $taototoCity->name)->find();
                if (!$data) {
                    $error[] = $taototoCity->getAttr('name');
                }
            });

        var_dump($error);


        exit;

        Log::error('测试报错');

        exit;

        $obj = new CouponSyncOrderService();
        $obj->products_serv = $obj->product_serv();
        $obj->syncOrder(PRODUCT_METUAN_SERV);
        throw new Exception();

        CouponOrder::query()
            ->where('serv_type', PRODUCT_COUPON_SERV)
            ->where('pay_status', CouponOrder::C_PAY_STATUS_NO)
            ->where('type', CouponOrder::TYPE_COUPON_SELF_EMPLOYED)
            ->where('status', CouponOrder::C_STATUS_NO_SUBMIT)
            ->where('created_at', '<', Carbon::now()->subMinutes(60))
            ->select()->each(function (CouponOrder $couponOrder) {
                $couponOrder->where('pay_status', CouponOrder::C_PAY_STATUS_NO)
                    ->update([
                        'status' => CouponOrder::C_STATUS_TIMEOUT_CLOSE
                    ]);
            });

        throw new Exception();

//        $this->refund();exit;

        CouponOrder::query()
            ->where('serv_type', PRODUCT_COUPON_SERV)
            ->where('pay_status', CouponOrder::C_PAY_STATUS_NO)
            ->where('status', CouponOrder::C_STATUS_NO_SUBMIT)
            ->where('type', CouponOrder::TYPE_COUPON_SELF_EMPLOYED)
            ->where('created_at', '<', Carbon::now()->subMinutes(1))
            ->select()->each(function (CouponOrder $couponOrder) {
                $couponOrder->where('pay_status', CouponOrder::C_PAY_STATUS_NO)
                    ->save([
                        'status' => CouponOrder::C_STATUS_TIMEOUT_CLOSE
                    ]);
            });
//
//        $request = new Request();
//
//        $pips[] = [\app\common\service\order\pipeline\Test::class,'handle'];
//        $pips[] = [\app\common\service\order\pipeline\Test2::class,'handle'];
//
//        (new Pipeline())
//            ->send($request)
//            ->through($pips)
//            ->then(function ($e) {
//
//            });
//
//        exit;
//
//        $obj = new CouponSyncOrderService();
//
//        $obj->products_serv = $obj->product_serv();
//
//        $obj->syncOrder(PRODUCT_FILM_SERV);

    }


    public function refund()
    {
        $couponOrder = CouponOrder::query()->where('id', 71203)->find();
        $data = event(new OrderPayRefund($couponOrder));

    }
}



